<?php

namespace App\Handler\Admin\ContactForm\Sale;

if (!defined('IN_PX'))
    exit;

use App\Handler\Admin\AbstractCommon;
use App\Admin\Helper;
use App\Tools\Auxi;
use Phoenix\Support\MsgHelper;

/**
 * 读取
 */
class Read extends AbstractCommon {

    public function processRequest(Array & $context) {
        $this->_pushSetting();

        $_start = ($_POST['page'] - 1) * $_POST['rp'];

        $_where = '0 = 0 AND `type_id` = 0';
        $_bindParam = array();
        if (isset($_POST['strSearchKeyword']) && $_POST['strSearchKeyword'] != '') {
            $_where .= ' AND ((`name` LIKE :strSearchKeyword)
            OR (`option` LIKE :strSearchKeyword)
            OR (`mobile` LIKE :strSearchKeyword))';
            $_bindParam[':strSearchKeyword'] = '%' . trim($_POST['strSearchKeyword']) . '%';
        }

        $_table = '`#@__@contact_form`';
        $_total = $this->db->table($_table)->where($_where)->bind($_bindParam)->count();

        $_rs = $this->db->select('*')
            ->table($_table)
            ->where($_where)
            ->order($_POST['sortName'], $_POST['sortOrder'])
            ->limit($_start, $_POST['rp'])
            ->bind($_bindParam)
            ->findAll();

        $_rsp = array(
            'totalResults' => $_total,
            'rows' => array()
        );
        if ($_total) {
            foreach ($_rs as $m) {
                $_idValue = $m->contact_id;
                array_push($_rsp['rows'], array(
                    'id' => $_idValue,
                    'cell' => array(
                        $_idValue,
//                        $m->option,
                        $m->name,
                        $m->mobile,
                        $m->email,
                        $m->content,
                        Auxi::getTime($m->add_date),
                        Auxi::getTime($m->release_date),
                    )
                ));
            }
        }
        echo(MsgHelper::json('SUCCESS', '数据返回成功', $_rsp));
    }

}
